package com.bw.module_inspection.view

import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.widget.PopupMenu
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_base.path.RouterPath
import com.bw.lib_base.view.BaseActivity
import com.bw.module_inspection.R
import com.bw.module_inspection.adapter.FragmentAdapter
import com.bw.module_inspection.databinding.ActivityInspectionDetailBinding
import com.bw.module_inspection.model.InspectionEntity
import com.bw.module_inspection.view.fragment.Inspection_RecordFragment
import com.bw.module_inspection.view.fragment.Inspection_standardsFragment
import com.bw.module_inspection.view.fragment.Work_Order_RecordFragment
import com.bw.module_inspection.viewmodel.InspectionViewModel
import com.google.android.material.tabs.TabLayoutMediator

/**
 * 智慧园区-07-巡检管理V1.1-史建军-20250515
 */
@Route(path = RouterPath.PATH_INSPECTION_DETAIL_ACTIVITY)
class InspectionDetailActivity : BaseActivity<ActivityInspectionDetailBinding>() {
    var inspectionEntity: InspectionEntity? = null
    private lateinit var fragmentAdapter: FragmentAdapter
    private var fragments = arrayListOf<Fragment>()
    private val vm by viewModels<InspectionViewModel> {ViewModelProvider.NewInstanceFactory()}
    var name:String=""
    var location:String=""
    @Autowired(name = "id")  // 自动注入
    @JvmField   // Kotlin 中需加此注解避免私有化
    var id: Int = 0  // 字段名必须与跳转时的 key（"id"）一致

    override fun initView() {
        ARouter.getInstance().inject(this)
        vm.getInspectionDetail(id)
        vm.inspectionDetailSuccess.observe(this){
          name=it.pointName
            location=it.projectId
            mDataBinding.tvProjectName.text=it.projectId
            mDataBinding.tvPointName.text=it.pointName
            mDataBinding.tvPointLocations.text=it.pointLocation
        }

        mDataBinding.topLayout.tvAdd.visibility= View.GONE
        mDataBinding.topLayout.ivBack.setOnClickListener {
            finish()
        }
        mDataBinding.topLayout.tvGoBack.text="点位详情"
        mDataBinding.topLayout.ivMenus.visibility=View.VISIBLE
        mDataBinding.topLayout.ivMenus.setOnClickListener {

        }
        fragments.add(Inspection_standardsFragment())
        fragments.add(Inspection_RecordFragment())
        fragments.add(Work_Order_RecordFragment())
        fragmentAdapter= FragmentAdapter(this,fragments)
        mDataBinding.vp2.adapter=fragmentAdapter
        TabLayoutMediator(
            mDataBinding.tabLayout, mDataBinding.vp2
        ) { tab, position ->
            when (position) {
                0 -> tab.text = "巡检标准"
                1 -> tab.text = "巡检记录"
                2 -> tab.text = "工单记录"
            }
        }.attach()
        mDataBinding.vp2.setUserInputEnabled(false);
        mDataBinding.topLayout.ivMenus.setOnClickListener {
            var popupMenu=PopupMenu(this,it)
            popupMenu.menuInflater.inflate(R.menu.menu_agent, popupMenu.menu)
            popupMenu.setOnMenuItemClickListener { itemView->
                when(itemView.itemId){
                    R.id.menu_agent1->{
                        ARouter.getInstance().build(RouterPath.PATH_INSPECTION_AGENT_ACTIVITY).withString("location",location).withString("name",name).navigation()
                        true
                    }else-> true
                }
            }
            popupMenu.show()
        }







    }

    override fun getLayoutId(): Int =R.layout.activity_inspection_detail

}